package terra.network.packet.server;

import org.jboss.netty.buffer.ChannelBuffer;

import terra.model.Slot;
import terra.network.packet.Packet;

public class InventoryInfo extends Packet {

	public int oId;
	public int constructable = -1;
	public int maxCount;
	
	public int[] slotItemIds = new int[Slot.SLOT_COUNT];
	public int[] slotOIds = new int[Slot.SLOT_COUNT];
	
	public int[] invItemIds;
	public int[] invOIds;
	public int[] invCounts;
	
	public InventoryInfo() {
	}
	
	@Override
	public ChannelBuffer toBuffer() {
		ChannelBuffer buf = getNewBuffer(1+4+12+8*Slot.SLOT_COUNT+12*maxCount);
		buf.writeByte(getId());
		buf.writeInt(12+8*Slot.SLOT_COUNT+12*maxCount);
		buf.writeInt(oId);
		buf.writeInt(constructable);
		buf.writeInt(maxCount);
		
		for(int i=0; i < Slot.SLOT_COUNT;i++){
			buf.writeInt(slotItemIds[i]);
			buf.writeInt(slotOIds[i]);
		}
		for(int i=0; i< maxCount;i++){
			buf.writeInt(invItemIds[i]);
			buf.writeInt(invOIds[i]);
			buf.writeInt(invCounts[i]);
		}
		return buf;
	}
	
	@Override
	public Packet fromBuffer(ChannelBuffer buf) {
		//int length = buf.readInt()-12-8*Slot.SLOT_COUNT;
		buf.readInt();
		oId = buf.readInt();
		constructable = buf.readInt();
		maxCount = buf.readInt();
		
		for(int i=0; i < Slot.SLOT_COUNT;i++){
			slotItemIds[i] = buf.readInt();
			slotOIds[i]= buf.readInt();
		}	
		invItemIds = new int[maxCount];
		invOIds = new int[maxCount];
		invCounts = new int[maxCount];
		
		for(int i=0; i < maxCount;i++){
			invItemIds[i] = buf.readInt();
			invOIds[i]= buf.readInt();
			invCounts[i]= buf.readInt();
		}			
		return this;
	}
	
	@Override
	public int getLength() {
		return 0;
	}
	
	@Override
	public boolean isDynamicLength() {
		return true;
	}
	
	@Override
	public byte getId() {
		return 17;
	}
	
	@Override
	public byte getPriority() {
		return PRIOR_NORMAL;
	}
	
	public String toString(){
		return "InventoryInfo: oId="+oId+"  constructable="+constructable+"  maxCount="+maxCount;
	}

}
